<template>
    <div class="container">
      <div class="tips-title">1.基础用法</div>
      <el-tabs v-model="activeName1" @tab-click="handleClick">
        <el-tab-pane label="用户管理" name="first"></el-tab-pane>
        <el-tab-pane label="配置管理" name="second"></el-tab-pane>
        <el-tab-pane label="角色管理" name="third"></el-tab-pane>
        <el-tab-pane label="定时任务补偿" name="fourth"></el-tab-pane>
      </el-tabs>
      <div class="tips-title">2.选项卡样式</div>
      <el-tabs v-model="activeName2" type="card" @tab-click="handleClick">
        <el-tab-pane name="first"><span slot="label"><i class="el-icon-user"></i> 用户管理</span></el-tab-pane>
        <el-tab-pane name="second"><span slot="label"><i class="el-icon-setting"></i> 配置管理</span></el-tab-pane>
        <el-tab-pane label="角色管理" name="third"></el-tab-pane>
        <el-tab-pane label="定时任务补偿" name="fourth"></el-tab-pane>
      </el-tabs>
      <div class="tips-title">3.卡片化</div>
      <el-tabs type="border-card">
        <el-tab-pane label="用户管理">用户管理</el-tab-pane>
        <el-tab-pane label="配置管理">配置管理</el-tab-pane>
        <el-tab-pane label="角色管理">角色管理</el-tab-pane>
        <el-tab-pane label="定时任务补偿">定时任务补偿</el-tab-pane>
      </el-tabs>
      <div class="tips-title">4.位置</div>
      <el-radio-group v-model="tabPosition" style="margin-bottom: 30px;">
        <el-radio-button label="top">top</el-radio-button>
        <el-radio-button label="right">right</el-radio-button>
        <el-radio-button label="bottom">bottom</el-radio-button>
        <el-radio-button label="left">left</el-radio-button>
      </el-radio-group>

      <el-tabs :tab-position="tabPosition" style="height: 200px;">
        <el-tab-pane label="用户管理">用户管理</el-tab-pane>
        <el-tab-pane label="配置管理">配置管理</el-tab-pane>
        <el-tab-pane label="角色管理">角色管理</el-tab-pane>
        <el-tab-pane label="定时任务补偿">定时任务补偿</el-tab-pane>
      </el-tabs>
      <div class="tips-title">5.动态增减标签页</div>
      <el-tabs v-model="editableTabsValue1" type="card" editable @edit="handleTabsEdit">
        <el-tab-pane :key="item.name" v-for="(item, index) in editableTabs1" :label="item.title" :name="item.name">
          {{item.content}}
        </el-tab-pane>
      </el-tabs>
      <div class="tips-title">6.自定义增加标签页触发器</div>
      <el-button size="small" type="button" @click="btnAddTab(editableTabsValue2)">add tab</el-button>
      <el-tabs v-model="editableTabsValue2" type="card" closable @tab-remove="removeTab">
        <el-tab-pane v-for="(item, index) in editableTabs2" :key="item.name" :label="item.title" :name="item.name">
          {{item.content}}
        </el-tab-pane>
      </el-tabs>
    </div>
</template>
<script>
  const testData1 = [
    {title: 'Tab 1',name: '1-1',content: 'Tab 1 content'},
    {title: 'Tab 2',name: '1-2',content: 'Tab 2 content'},
    {title: 'Tab 3',name: '1-3',content: 'Tab 3 content'},
    {title: 'Tab 4',name: '1-4',content: 'Tab 4 content'}
  ];
  const testData2 = [
    {title: 'Tab 1',name: '2-1',content: 'Tab 1 content'},
    {title: 'Tab 2',name: '2-2',content: 'Tab 2 content'},
    {title: 'Tab 3',name: '2-3',content: 'Tab 3 content'},
    {title: 'Tab 4',name: '2-4',content: 'Tab 4 content'}
  ];
  export default {
    name: 'na-tabs',
    data() {
      return {
        activeName1: 'second',
        activeName2: 'first',
        tabPosition: 'left',

        editableTabsValue1: '2',
        editableTabs1: testData1,
        tabIndex1: 2,

        editableTabsValue2: '2',
        editableTabs2: testData2,
        tabIndex2: 2
      }
    },
    methods: {
      handleClick(tab, event) {
        console.log(tab, event);
      },
      handleTabsEdit(targetName, action) {
        if (action === 'add') {
          let newTabName = ++this.tabIndex1 + '';
          this.editableTabs1.push({
            title: 'New Tab',
            name: newTabName,
            content: 'New Tab content'
          });
          this.editableTabsValue1 = newTabName;
        }
        if (action === 'remove') {
          let tabs = this.editableTabs1;
          let activeName = this.editableTabsValue1;
          if (activeName === targetName) {
            tabs.forEach((tab, index) => {
              if (tab.name === targetName) {
                let nextTab = tabs[index + 1] || tabs[index - 1];
                if (nextTab) {
                  activeName = nextTab.name;
                }
              }
            });
          }
          this.editableTabsValue1 = activeName;
          this.editableTabs1 = tabs.filter(tab => tab.name !== targetName);
        }
      },
      btnAddTab(targetName) {
        let newTabName = ++this.tabIndex2 + '';
        this.editableTabs2.push({
          title: 'New Tab',
          name: newTabName,
          content: 'New Tab content'
        });
        this.editableTabsValue2 = newTabName;
      },
      removeTab(targetName) {
        let tabs = this.editableTabs2;
        let activeName = this.editableTabsValue2;
        if (activeName === targetName) {
          tabs.forEach((tab, index) => {
            if (tab.name === targetName) {
              let nextTab = tabs[index + 1] || tabs[index - 1];
              if (nextTab) {
                activeName = nextTab.name;
              }
            }
          });
        }
        this.editableTabsValue2 = activeName;
        this.editableTabs2 = tabs.filter(tab => tab.name !== targetName);
      }
    }
  }
</script>
<style>
</style>
